package callbackframework;


import com.sun.opengl.util.FPSAnimator;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * StereoPairViewer.java <BR>
 *
 * @author: rob
 * @author: Ideas taken from Andrew Davison's Callback Animation Framework as outlined in
 * Pro Java 6 Game Development
 */
public class StereoPairViewer extends JFrame {

    private static int DEFAULT_FPS = 60; //target fps that FPSAnimator class trys to achieve
    private GLCapabilities caps;                                     //ive noticed that it cant go above 1000fps and crashes because of the timer fpsanimator uses which must be milliseconds

    private static final int PWIDTH = 1024;   // initial size of jpanel that sits inside JFrame
    private static final int PHEIGHT = 512;

    private StereoPairGLListener listener; //implements opengl GLEventListener 
    private FPSAnimator animator; //creates a loop that calls the display method in GLCanvas over and over

    private JTextField instructions;

    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
     private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItemQuit;

    public StereoPairViewer(int fps)
    {
        super("3D Stereo Pair Viewer"); //JFrame title

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItemQuit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenuItemQuit.setText("Quit");
        jMenuItemQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemQuitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemQuit);

        jMenu2.setText("Primitives");

        jMenuItem1.setText("Cube");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem2.setText("Plane");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem3.setText("Sphere");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuItem4.setText("Cone");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

         jMenuItem5.setText("Cube on Plane");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        Container c = getContentPane();
        c.setLayout( new BorderLayout() );
        c.add(makeRenderPanel(fps), BorderLayout.CENTER); //add a JPanel to the JFrame that contains an opengl GLCanvas

        instructions = new JTextField("1.Rotate object with arrow keys.  2.Toggle wireframe mode with the 'm' key");
        instructions.setEditable(false);
        c.add(instructions, BorderLayout.SOUTH);

        addWindowListener( new WindowAdapter() {
        public void windowClosing(WindowEvent e)        
        { new Thread( new Runnable() {
          public void run() {
            animator.stop();
            System.exit(0);
          }
        }).start();
        } // end of windowClosing()
       });

        pack();
        setVisible(true);

        animator.start();

    }

     private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        listener.getPrimitive().setPrimitive("cube");
    }
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
        listener.getPrimitive().setPrimitive("plane");
    }
     private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
        listener.getPrimitive().setPrimitive("sphere");
    }
      private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
        listener.getPrimitive().setPrimitive("cone");
    }
       private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
        listener.getPrimitive().setPrimitive("cubeonplane");
    }
      private void jMenuItemQuitActionPerformed(java.awt.event.ActionEvent evt) {
         System.exit(0);
    }

    /**
     * Construct a GLCanvas in a JPanel, and add a
     * listener and animator. 
     * @param fps
     * @return JPanel with GLCanvas in it
     */
    private JPanel makeRenderPanel(int fps)
    {
    JPanel renderPane = new JPanel();   // for GLCanvas   

    renderPane.setLayout( new BorderLayout() );
    renderPane.setOpaque(false);
    renderPane.setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
    caps = new GLCapabilities();
    caps.setSampleBuffers(true);
    caps.setNumSamples(4); //antialiasing
    GLCanvas canvas = new GLCanvas(caps);
    
    listener = new StereoPairGLListener(this);
    canvas.addGLEventListener(listener);

    animator = new FPSAnimator(canvas, fps, true);        

    renderPane.add(canvas, BorderLayout.CENTER);
    
    return renderPane;
    }  // end of makeRenderPanel()




    public static void main(String[] args)
   {
     new StereoPairViewer(DEFAULT_FPS);
   }  // end of main()
    

}